package com.cr.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cr.model.Cloth;
import com.cr.util.DBUtil;

/**
 * 
 * @author Administrator
 *
 */
public class ClothDao {
	private DBUtil db=new DBUtil();
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	public Cloth getCloth(long id)
	{
		Cloth cloth=null;
		
		String sql="select * from cloth where Id=?";
		Object[] args={id};
		ResultSet rs=db.preQuery(sql, args);
		
		if(rs!=null)
		{
			try {
				if(rs.next())
				{
					cloth=new Cloth();
					
					cloth.setColor(rs.getString("Color"));
					cloth.setDescription(rs.getString("Description"));
					cloth.setId(id);
					cloth.setName(rs.getString("Name"));
					cloth.setPrice(rs.getFloat("Price"));
					cloth.setSize(rs.getString("Size"));
					cloth.setStyle(rs.getString("Style"));
					cloth.setMaterial(rs.getString("Material"));
					List<String> picture=null;
					String[] strs={};
					if(rs.getString("Picture")!=null)
					{
						strs=rs.getString("Picture").split(";");
					}
					
					picture=new ArrayList<String>();
					for(String str:strs)
					{
						picture.add(str);
					}
					cloth.setPicture(picture);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return cloth;
	}
	/**
	 * 
	 * @param cloth
	 * @return
	 */
	public boolean addCloth(Cloth cloth)
	{
		boolean flag=false;
		String sql="insert into cloth(Color,Description,Name,Price,Size,Style,Material,Picture) ";
		sql+=" values(?,?,?,?,?,?)";
		
		String picture="";
		List<String> strs=cloth.getPicture();
		if(strs!=null && strs.size()>0)
		{
			for(String str:strs)
			{
				picture+=str+";";
			}
			picture=picture.substring(0,picture.length()-1);
		}
		
		Object[] args={cloth.getColor(),
				cloth.getDescription(),
				cloth.getName(),
				cloth.getPrice(),
				cloth.getSize(),
				cloth.getStyle(),
				cloth.getMaterial(),
				picture};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}
	/**
	 * 
	 * @param cloth
	 * @return
	 */
	public boolean updateCloth(Cloth cloth)
	{
		boolean flag=false;
		String sql="update cloth set ";
		sql+=" Color=?,";
		sql+=" Description=?,";
		sql+=" Name=?,";
		sql+=" Price=?,";
		sql+=" Size=?,";
		sql+=" Style=?,";
		sql+=" Material=?,";
		sql+=" Picture=?";
		sql+=" where Id=?";
		
		String picture="";
		List<String> strs=cloth.getPicture();
		if(strs!=null && strs.size()>0)
		{
			for(String str:strs)
			{
				picture+=str+";";
			}
			picture=picture.substring(0,picture.length()-1);
		}
		Object[] args={cloth.getColor(),
				cloth.getDescription(),
				cloth.getName(),
				cloth.getPrice(),
				cloth.getSize(),
				cloth.getStyle(),
				cloth.getMaterial(),
				picture,
				cloth.getId()};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	public boolean deleteCloth(long id)
	{
		boolean flag=false;
		String sql="delete from cloth where Id=?";
		
		Object[] args={id};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}
	/**
	 * 
	 * @param condition
	 * @return
	 */
	public List<Cloth> getClothes(String condition)
	{
		List<Cloth> list=null;
		
		String sql="select * from cloth";
		if(!condition.equals(""))
		{
			sql+=" where "+condition;
		}
		
		ResultSet rs=db.preQuery(sql, null);
		
		if(rs!=null)
		{
			try {
				list=new ArrayList<Cloth>();
				while(rs.next())
				{
					Cloth cloth=new Cloth();
					
					cloth.setColor(rs.getString("Color"));
					cloth.setDescription(rs.getString("Description"));
					cloth.setId(rs.getLong("Id"));
					cloth.setName(rs.getString("Name"));
					cloth.setPrice(rs.getFloat("Price"));
					cloth.setSize(rs.getString("Size"));
					cloth.setStyle(rs.getString("Style"));
					cloth.setMaterial(rs.getString("Material"));
					List<String> picture=null;
					String[] strs={};
					if(rs.getString("Picture")!=null)
					{
						strs=rs.getString("Picture").split(";");
					}
					
					picture=new ArrayList<String>();
					for(String str:strs)
					{
						picture.add(str);
					}
					cloth.setPicture(picture);
					
					list.add(cloth);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
